<?php


namespace App\Models;


class Vote extends BaseModel
{
    //获取正在举办的活动
    public static function getVoting($status)
    {
        $vote = self::where('vote_status', $status)->orderBy('id','desc')->first();
        if ($vote) {
            $vote->increment('view_count');
            $vote = $vote->toArray();
            $time = $status == 1 ? strtotime($vote['begin_at']) : time();
            $time = strtotime($vote['end_at']) - $time;
            $timestamp = $time > 0 ? $time : 0;
            $vote['timestamp'] = $timestamp;
        }
        return $vote;
    }

    public static function getVoteTime($vote_id)
    {
        $vote = self::find($vote_id);
        if ($vote) {
            $time = $vote['vote_status'] == 1 ? strtotime($vote['begin_at']) : time();
            $time = strtotime($vote['end_at']) - $time;
            $timestamp = $time > 0 ? $time : 0;
            return $timestamp;
        }
        return 0;
    }
}